/**
 * 
 */
package logger;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;


/**
 * @author Administrator
 *
 */
public class ForumLogger {

	private static Logger _logger;
	/**
	 * @param name
	 * @param resourceBundleName
	 */
	private ForumLogger() {

		_logger = Logger.getLogger("MyLog");
		FileHandler fh;

		try {

			// This block configure the logger with handler and formatter
			fh = new FileHandler("LogFile.log", true);
			_logger.addHandler(fh);
			_logger.setLevel(Level.ALL);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);

		} catch (SecurityException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		    }
	}

	public static void logRegisterUser(String userName){
		getInstance().fine(userName + " has joined the WSEP Forum");
	}

	public static void logLogin(String userName){
		getInstance().fine(userName + " just logged in");
	}

	public static void logLogout(String userName){
		getInstance().fine(userName + " just logged out");
	}
	

	public static void logAddPost(String userName, String postSubject){
		getInstance().fine(userName + " has added the post \" " + postSubject + " \"");
	}

	public static void logUpdatePost(String userName, String newSubject, String oldSubject){
		if (newSubject.equals(oldSubject))
			getInstance().fine(userName + " has updated the post \" " + newSubject + " \"");
		else
			getInstance().fine(userName + " has updated the post \" " + oldSubject + 
											" \" and its new subject is \" " + newSubject + " \" ");
	}
	
	public static void logDeletePost(String userName, String postSubject){
		getInstance().fine(userName + " has deleted the post \" " + postSubject + " \"");
	}

	public static void logIOException(String userName){
		getInstance().severe("IOException has occurred to " + userName);
	}

	public static void logIOException(){
		getInstance().severe("IOException has occurred" );
	}
	
	// the following statement is used to log any messages
	public static void logGeneral(String logMessage) {
		getInstance().fine(logMessage);
	}
	
	public static void logLoginFail(String username) {
		getInstance().fine("authentication failure for user "+username);
	}
	
	public static Logger getInstance(){
		if (_logger==null){
			new ForumLogger();
		}
		return _logger;
	}

	public static void logAddPostFail(String userName, String subject) {
		getInstance().fine("add new post failure for: "+userName + "post subject: "+subject);		
	}

	public static void logUpdatePostFail(String userName, String newSubject,
			String oldSubject) {
		getInstance().fine("update new post failure for: "+userName + "post subject: "+oldSubject);		
		
	}

	public static void logDeletePostFail(String userName, String subject) {
		getInstance().fine("delete post failure for user: "+userName+" subject: "+subject);
		
	}

	public static void logRegisterFail(String username) {
		getInstance().fine("registration failure for username "+username);
		
	}
	
	public static void logUpdate(String username) {
		getInstance().fine(username+" just update his/her details");
		
	}
	
	public static void logUpdateFail(String username) {
		getInstance().fine("update failure for user " +username);
		
	}

}
